

#import "MineViewController.h"
#import "MineHeader.h"
#import "MineFooter.h"

#import "LoginViewController.h"
#import "PrivateViewController.h"
@interface MineViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self initUI];
    [NotifyTool add:self :@selector(loadData) :notifyUserUpdate];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)initUI
{
    self.backBtn.hidden = YES;
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.c_edges.equalTo(self.view).coffset(UIEdgeInsetsMake(0, 0, -TabBarHight, 0));
}
- (void)loadData{
    [self.tableView reloadData];
}
- (void)action:(NSString *)title{
    WS(self)
    if ([title isEqualToString:LS(@"去登录")]) {
        LoginViewController * vc = [LoginViewController new];
        vc.islogin = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:@"更换头像"]){
        SheetViewController *sheetvc = [SheetViewController new];
        sheetvc.withCancel = NO;
        sheetvc.array = @[@"拍摄",@"相册"];
        sheetvc.block = ^(int index, NSString * _Nonnull text) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [CCHud startHud];
            });
            UIImagePickerController *vc = [UIImagePickerController new];
            vc.delegate = weakself;
            vc.allowsEditing = YES;
            vc.sourceType = index == 0 ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [[weakself currentVC] presentViewController:vc animated:YES completion:^{
                [CCHud stopHud];
            }];
        };
        [[weakself currentVC] presentViewController:sheetvc animated:NO completion:nil];
    }else if ([title isEqualToString:@"清理缓存"]){
        [CCHud startHud:LS(@"清理缓存中...") stopTime:2 handle:^{
            [CCHud msg:LS(@"已清理干净")];
        }];
    }else if ([title isEqualToString:@"帮助反馈"]){
        FeedBackViewController * vc= [FeedBackViewController new];
        vc.title = LS(@"帮助反馈");
        [self push:vc :YES];
    }else if ([title isEqualToString:@"隐私政策"]){
        PrivateViewController * vc= [PrivateViewController new];
        [self push:vc :YES];
    }else if ([title isEqualToString:LS(@"退出登录")]){
        APP.user = [User new];
    }else if ([title isEqualToString:LS(@"注销账号")]){
        [self alert:LS(@"注销账号") :LS(@"您确定要注销账号吗？") :^{
        } :^{
            [UserTool delete:APP.user complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    APP.user = [User new];
                }
            }];
        }];
    }
}

#pragma mark - UIImagePickerControllerDelegate / UINavigationControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    UIImage *img = info[UIImagePickerControllerEditedImage];
    APP.user.headImg = UIImageJPEGRepresentation(img, 0.5);
    [UserTool update:APP.user complete:^(NSDictionary * _Nonnull dict) {
        
    }];
    [self loadData];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return [APP.user.account isEqualToString:defaulUserID] ? 0 : 134;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 300.0/375*SCREEN_WIDTH + 62 * 3;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if ([APP.user.account isEqualToString:defaulUserID]) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 134)];
    view.backgroundColor = [UIColor clearColor];
    MineFooter *header = (MineFooter *)[MineFooter loadFromXIB];
    header.frame = view.bounds;
    WS(self)
    header.block = ^(NSString * _Nonnull title) {
        [weakself action:title];
    };
    [view addSubview:header];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300.0/375*SCREEN_WIDTH + 62 * 3)];
    view.backgroundColor = [UIColor clearColor];
    MineHeader *header = (MineHeader *)[MineHeader loadFromXIB];
    header.frame = view.bounds;
    WS(self)
    header.block = ^(NSString * _Nonnull title) {
        [weakself action:title];
    };
    [view addSubview:header];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}

@end

